<template>
  <div class="app-container">
    <!--搜索栏 开始-->
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <!--      <el-form-item label="文章标题" prop="title">
              <el-input
                v-model="queryParams.title"
                placeholder="请输入文章标题"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">刷新</el-button>
            </el-form-item>-->
    </el-form>
    <!--搜索栏 结束-->

    <!--操作栏 开始-->
    <!--    <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['mini:wenzhang:add']"
            >新增
            </el-button>
          </el-col>
        </el-row>-->
    <!--操作栏 结束-->

    <el-table v-loading="loading" :data="tableData">
      <el-table-column
        sortable
        align="center"
        prop="userId"
        label="用户ID"
      >
      </el-table-column>
      <el-table-column
        sortable
        align="center"
        prop="userName"
        label="用户昵称"
      >
      </el-table-column>
      <el-table-column
        align="center"
        prop="txt"
        width="600"
        show-overflow-tooltip
        label="内容"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="审核状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        sortable
        align="center"
        prop="zan"
        label="点赞数"
      >
      </el-table-column>
      <el-table-column
        sortable
        align="center"
        prop="time"
        width="250"
        label="时间"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
<!--        <template slot-scope="scope" v-if="scope.row.userId !== 1">-->
        <template slot-scope="scope" >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            :disabled="scope.row.source == '1'"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:user:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            :disabled="scope.row.source == '1'"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:user:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改部门对话框 -->
    <!--
      ref="form" : 校验时通过该属性找到对应的表单（主要用于表单提交时，判断校验规则是否否满足）
      rules="rules： 表单校验时，查询具体的校验规则列表
        rules.prop 查找属性指定的校验规则
      :model="form" 表单数据和data属性的映射关系
    -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="用户名" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入用户名"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="点赞数" prop="zan">
              <el-input v-model="form.zan" placeholder="请输入点赞数"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import config from '@/utils/config'

import {
  listPyq,
  updatePyq,
  delPyq
} from '@/api/mini/pyq.js'
import {getUser} from "@/api/system/user";
import {getDept, listDeptExcludeChild} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";

export default {
  name: 'MiniPyq',
  components: {Treeselect},
  data() {
    return {
      imgPrex: config.imgPrefx,
      loading: true,
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      total: 0,
      tableData: [],
      title: '',
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      form: {},
      rules: {
        userName: [
          {required: true, message: '用户名不能为空', trigger: 'blur'}
        ],
        zan: [
          {required: true, message: '点赞数不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      this.resetForm()
      listPyq(this.queryParams).then(response => {
        this.tableData = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    cancel() {
      this.open = false
      this.resetForm()
    },
    resetForm() {
      this.form = {}
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updatePyq({
            "id": this.form.id,
            "userName": this.form.userName,
            "zan": this.form.zan
          }).then((res=>{
            this.$modal.msgSuccess( '成功')
            this.open = false;
            this.getList()
          }))
        }
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({name: 'WenzhangModify'})
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.resetForm();
      this.open = true;
      listPyq({
        id: row.id
      }).then(res=>{
        this.form = res.rows[0]
        this.form.id = row.id
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const keyIds = row.id || this.ids
      this.$confirm('是否删除编号"' + keyIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delPyq (keyIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    },
    /** 启用，停用 */
    handleStatusChange(row) {
      let text = row.status === '0' ? '驳回' : '通过'
      this.$modal.confirm('确认要' + text + "该记录吗？").then(function () {
        return updatePyq({
          "id": row.id,
          "status": row.status
        })
      }).then(() => {
        this.$modal.msgSuccess(text + '成功')
        this.getList()
      }).catch(function () {
        row.status = row.status === '0' ? '1' : '0'
      })
    }
  }
}
</script>
